# First, let's make sure that we're on Linux. If we're not, there's no point.

IF(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
	MESSAGE("System: ${CMAKE_SYSTEM_NAME}")
	MESSAGE(FATAL_ERROR "Not on a Linux system, LinPulse doesn't work.")
ENDIF(NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")

# Include some useful stuff

INCLUDE(PkgConfig.cmake)
INCLUDE(CheckIncludeFileCXX)

# Set some options

SET(EXECUTABLE_OUTPUT_PATH "../build/")
INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/linpulse DESTINATION bin)

# Check for libraries

FIND_PACKAGE( Boost COMPONENTS thread )
IF(NOT Boost_FOUND)
  MESSAGE(FATAL_ERROR "Cannot find libboost-thread.")
ELSE(NOT Boost_FOUND)
   SET(CMAKE_CXX_LINK_FLAGS "-lboost_thread -L${Boost_LIBRARY_DIRS} ${CMAKE_CXX_LINK_FLAGS}")
ENDIF(NOT Boost_FOUND)

# I know this isn't the best way to approach pkg-config and I should use Find_Package, but
# macros don't exist for all the libraries I'm using, so I'll keep doing it this way for now.

PKGCONFIG("libconfig++ >= 1.3")
IF(PKGCONFIG_FOUND)
  IF(CMAKE_PKGCONFIG_CXX_FLAGS)
    SET(CMAKE_CXX_FLAGS "${CMAKE_PKGCONFIG_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
  ENDIF(CMAKE_PKGCONFIG_CXX_FLAGS)
  IF(CMAKE_PKGCONFIG_C_FLAGS)
    SET(CMAKE_C_FLAGS "${CMAKE_PKGCONFIG_C_FLAGS} ${CMAKE_C_FLAGS}")
  ENDIF(CMAKE_PKGCONFIG_C_FLAGS)
  IF(PKGCONFIG_LIBRARIES)
	SET(CMAKE_CXX_LINK_FLAGS "${PKGCONFIG_LIBRARIES} ${CMAKE_CXX_LINK_FLAGS}")
  ENDIF(PKGCONFIG_LIBRARIES)
  MESSAGE("Found libconfig++")
ELSE(PKGCONFIG_FOUND)
  MESSAGE(FATAL_ERROR "Cannot find libconfig++ version 1.3 or higher")
ENDIF(PKGCONFIG_FOUND)

PKGCONFIG("openssl >= 0.9.8g")
IF(PKGCONFIG_FOUND)
  IF(CMAKE_PKGCONFIG_CXX_FLAGS)
    SET(CMAKE_CXX_FLAGS "${CMAKE_PKGCONFIG_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
  ENDIF(CMAKE_PKGCONFIG_CXX_FLAGS)
  IF(CMAKE_PKGCONFIG_C_FLAGS)
    SET(CMAKE_C_FLAGS "${CMAKE_PKGCONFIG_C_FLAGS} ${CMAKE_C_FLAGS}")
  ENDIF(CMAKE_PKGCONFIG_C_FLAGS)
  IF(PKGCONFIG_LIBRARIES)
	SET(CMAKE_CXX_LINK_FLAGS "${PKGCONFIG_LIBRARIES} ${CMAKE_CXX_LINK_FLAGS}")
  ENDIF(PKGCONFIG_LIBRARIES)
  MESSAGE("Found openssl")
ELSE(PKGCONFIG_FOUND)
  MESSAGE(FATAL_ERROR "Cannot find openssl 0.9.8g or higher")
ENDIF(PKGCONFIG_FOUND)

PKGCONFIG("libpcrecpp >= 7.4")
IF(PKGCONFIG_FOUND)
  IF(CMAKE_PKGCONFIG_CXX_FLAGS)
    SET(CMAKE_CXX_FLAGS "${CMAKE_PKGCONFIG_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
  ENDIF(CMAKE_PKGCONFIG_CXX_FLAGS)
  IF(CMAKE_PKGCONFIG_C_FLAGS)
    SET(CMAKE_C_FLAGS "${CMAKE_PKGCONFIG_C_FLAGS} ${CMAKE_C_FLAGS}")
  ENDIF(CMAKE_PKGCONFIG_C_FLAGS)
  IF(PKGCONFIG_LIBRARIES)
	SET(CMAKE_CXX_LINK_FLAGS "${PKGCONFIG_LIBRARIES} ${CMAKE_CXX_LINK_FLAGS}")
  ENDIF(PKGCONFIG_LIBRARIES)
  MESSAGE("Found libpcrecpp")
ELSE(PKGCONFIG_FOUND)
  MESSAGE(FATAL_ERROR "Cannot find pcrecpp 7.4 or higher")
ENDIF(PKGCONFIG_FOUND)

PKGCONFIG("sqlite3 >= 3.5.9")
IF(PKGCONFIG_FOUND)
  IF(CMAKE_PKGCONFIG_CXX_FLAGS)
    SET(CMAKE_CXX_FLAGS "${CMAKE_PKGCONFIG_CXX_FLAGS} ${CMAKE_CXX_FLAGS}")
  ENDIF(CMAKE_PKGCONFIG_CXX_FLAGS)
  IF(CMAKE_PKGCONFIG_C_FLAGS)
    SET(CMAKE_C_FLAGS "${CMAKE_PKGCONFIG_C_FLAGS} ${CMAKE_C_FLAGS}")
  ENDIF(CMAKE_PKGCONFIG_C_FLAGS)
  IF(PKGCONFIG_LIBRARIES)
	SET(CMAKE_CXX_LINK_FLAGS "${PKGCONFIG_LIBRARIES} ${CMAKE_CXX_LINK_FLAGS}")
  ENDIF(PKGCONFIG_LIBRARIES)
  MESSAGE("Found sqlite3")
  SET(_HAVE_SQLITE3_ TRUE)
ELSE(PKGCONFIG_FOUND)
  MESSAGE("Cannot find sqlite3 3.5.9 or higher, won't be able to pulse to database.")
  SET(_HAVE_SQLITE3_)
ENDIF(PKGCONFIG_FOUND)

# Options

OPTION(DEBUG "Build the project using debugging code" FALSE)
IF(DEBUG)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DDEBUG -Wall -pedantic -ggdb")
ELSE(DEBUG)
  SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -funroll-loops -fomit-frame-pointer")
ENDIF(DEBUG)

# Print out our CXXFLAGS and CXX_LINK FLAGS

MESSAGE("")
MESSAGE("CXXFLAGS for the C++ compiler: ${CMAKE_CXX_FLAGS}")
MESSAGE("CXX_LINK_FLAGS for the linker: ${CMAKE_CXX_LINK_FLAGS}")
MESSAGE("")

# Check for include files/prototypes/etc

CHECK_INCLUDE_FILE_CXX("libintl.h" _HAVE_LIBINTL_H_)
CHECK_INCLUDE_FILE_CXX("boost/algorithm/string.hpp" _HAVE_BOOST_STRING_)
CHECK_INCLUDE_FILE_CXX("signal.h" _HAVE_SIGNAL_H_)

IF(NOT _HAVE_LIBINTL_H_)
	MESSAGE("Missing libintl.h, please install libc6-dev or whatever is an")
	MESSAGE("appropriate package for libintl.h and GNU gettext i18n/l10n.")
	MESSAGE("Without libintl.h, LinPulse will not have language-appropriate translations.")
ENDIF(NOT _HAVE_LIBINTL_H_)

IF(NOT _HAVE_BOOST_STRING_)
	MESSAGE(FATAL_ERROR "Missing boost/algorithm/string.hpp, a required component.")
ENDIF(NOT _HAVE_BOOST_STRING_)

IF(NOT _HAVE_SIGNAL_H_)
	MESSAGE(FATAL_ERROR "Missing signal.h, a required component.")
ENDIF(NOT _HAVE_SIGNAL_H_)

CONFIGURE_FILE(./include/Config.h.in ./include/Config.h)

ADD_SUBDIRECTORY(src)